﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Hosting.Internal;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using HelloCore.Extensions;

namespace HelloCore
{
    public class Startup
    {
        private readonly IConfiguration _configuration;
        public Startup(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var builder = new ContainerBuilder();

            builder.Populate(services);
            var container = builder.Build();
            return new AutofacServiceProvider(container);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IConfiguration config, IApplicationLifetime applicationLifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            applicationLifetime.ApplicationStarted.Register(() =>
            {
                Console.WriteLine("****************ApplicationStarted");
            });
            applicationLifetime.ApplicationStopping.Register(() =>
            {
                Console.WriteLine("****************ApplicationStopping");
            });
            applicationLifetime.ApplicationStopped.Register(() =>
            {
                Console.WriteLine("****************ApplicationStopped");
            });
            app.UseMiddleware<CustomMiddleWare>();
            app.Map("/Zj", handler =>
            {
                handler.Run(async context =>
                {
                    await context.Response.WriteAsync("MAP ONCE...");
                });
            });
            app.MapWhen(context => context.Request.Path.Value.Contains("a")
            , handler =>
            {
                handler.Run(async context =>
                {
                    await context.Response.WriteAsync("MAPWhen ONCE...");
                });
            });
            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("Use ONCE...");
                await next();
            });
            app.Run(async context =>
            {
                await context.Response.WriteAsync("RUN ONCE...");
            });
            //app.Run(async (context) =>
            //{
            //    var myConn = config["ConnectionStrings:DefaultConnection"];
            //    var name = config["name"];
            //    var classNo = config["class"];
            //    Console.WriteLine($"ConnectionString{myConn}");
            //    Console.WriteLine($"name:{name},\tclass:{classNo}");

            //    Console.WriteLine($"ApplicationName:{env.ApplicationName},\tContentRootPath:{env.ContentRootPath},\tEnvironmentName:{env.EnvironmentName},\tWebRootPath:{env.WebRootPath},\tContentRootFileProvider:{env.ContentRootFileProvider}");
            //    await context.Response.WriteAsync("Hello World!");
            //});


        }
    }
}
